$(function() {
	var carnum = 0;
	var page = 1;
	var loadarr = [];
	var tddata = [{
		key: 'order_no',
		type: 1,
		url: '{:url("detail")}',
		id: 137
	}, {
		key: 'order_type2',
		type: 1,
		url: '{:url("detail")}',
		id: 137
	}, {
		key: 'orderer',
		type: 1,
		url: ''
	}, {
		key: 'from_addr',
		type: 1,
		url: ''
	}, {
		key: 'to_addr',
		type: 1,
		url: ''
	}, {
		key: 'order_state2',
		type: 1,
		url: ''
	}];
	var addoperation = `<td>
						<div class="tpl-table-black-operation operate">
							<a href="javascript:;" class='detail'>
								<i class="am-icon-pencil"></i> 详情
							</a>
						</div>
					</td>`
	var carlist;
	caroption({page:1,pagenum:9999});
	var driverlist;
	driveroption({page:1,pagenum:9999});
	//滚动
	var myScroll = new IScroll('#scrollmain', {
		mouseWheel: true,
		disableMouse: true,
		disablePointer: true,
		scrollbars: 'custom',
		interactiveScrollbars: true,
	});
	ajax_quota_order(page)
	//车辆数
	
	function ajax_quota_order(page) {
		var quorderload = layer.load(0, {
			shade: [0.1, '#fff'] //0.1透明度的白色背景
		});
		$.ajax({
			type: "post",
			url: "{:url('adminapi/Bus/getBusList')}",
			data: {
				uid: '{$uid}',
				page: page,
				pagenum: 5
			},
			success: function(res) {
				layer.close(quorderload)
				if(res.code == 1) {
					for(var quoteidx = 0; quoteidx < res.data.length; quoteidx++) {
						if(res.data[quoteidx].order_type == 1) {
							res.data[quoteidx].order_type2 = '公务订单'
						} else if(res.data[quoteidx].order_type == 2) {
							res.data[quoteidx].order_type2 = '企业订单'
						} else if(res.data[quoteidx].order_type == 3) {
							res.data[quoteidx].order_type2 = '个人订单'
						}
						if(res.data[quoteidx].order_state == 21) {
							res.data[quoteidx].order_state2 = '待报价'
						} else if(res.data[quoteidx].order_state == 23) {
							res.data[quoteidx].order_state2 = '已报价';
							$('.keep').css('display', 'none')
							$('.keepdis').css('display', 'block')

						}
					}
					$('tbody').html(tbodymainf2(res.data, 1, tddata, addoperation, 'order_id'))
					$('.pagination').html(pagechang1(res.data.length, 5, page))
					$('.detail').on('click',function(){
						carnum=0
						$('.allcar').html("")
						$('input').each(function(){
							$(this).val("")
						})
						for(var oidx=0;oidx<res.data.length;oidx++){
							if(res.data[oidx].order_id==$(this).closest('tr').data('id')){
								console.log(res.data[oidx])
								$('.orderdetail').html(orderdetail(res.data[oidx]))
								$('.orderstate').html(orderstate(res.data[oidx]))
								$('input[name="order_id"]').val($(this).closest('tr').data('id'))
							}
						}
						$('.car_add').unbind()
						$('.car_add').on('click',function(){
							
							if(carnum>=$('input[name="car_num"]').val()){
								Err('匹配的车辆数多于订单所需车辆数')
							}else{
								carnum+=1
								addcar(carnum)
							}
							
							
						})
					})
					//订单详情
					function orderdetail(obj){
						var type=obj.is_return
						if(type==1){
							obj.is_return2='返程'
						}else{
							obj.is_return2='单程'
						}
						if(obj.booking_type==1){
							obj.booking_type2='订车（日常）'
						}else if(obj.booking_type==2){
							obj.booking_type2='包车'
						}else{
							obj.booking_type2='分时租赁'
						}
						if(obj.car_type==2){
							obj.car_type2='优选型'
						}else if(obj.car_type==3){
							obj.car_type2='舒适型'
						}else if(obj.car_type==4){
							obj.car_type2='豪华型'
						}else if(obj.car_type==5){
							obj.car_type2='大巴'
						}else if(obj.car_type==6){
							obj.car_type2='小巴'
						}else if(obj.car_type==7){
							obj.car_type2='中巴'
						}else if(obj.car_type==8){
							obj.car_type2='执法车'
						}else if(obj.car_type==9){
							obj.car_type2='商务型'
						}
						var detail=`
							<div class="am-u-sm-12 cl-p">
								<div class="order_key">订单编号：</div>
								<div class="order_value_black">${obj.order_no}</div>
								<input type='hidden' name='order_id' value="${obj.order_id}"/>
							</div>
							<div class="am-u-sm-12 cl-p">
								<div class="order_key">订单类型：</div>
								<div class="order_value_black">${obj.order_type2}</div>
							</div>
							<div class="am-u-sm-12 cl-p">
								<div class="order_key">订车类型：</div>
								<div class="order_value_black">${obj.booking_type2}</div>
							</div>
							<div class="am-u-sm-12 cl-p">
								<div class="order_key">车辆类型：</div>
								<div class="order_value_black">${obj.car_type2}</div>
							</div>
							
							<div class="am-u-sm-12 cl-p">
								<div class="order_key">出发地：</div>
								<div class="order_value_black">${obj.from_addr}</div>
							</div>
							<div class="am-u-sm-12 cl-p">
								<div class="order_key">目的地：</div>
								<div class="order_value_black">${obj.to_addr}</div>
							</div>
							<div class="am-u-sm-12 cl-p">
								<div class="order_key">乘客：</div>
								<div class="order_value_black">${obj.passenger_name}</div>
							</div>
						`;
						if(obj.booking_type==2){
							detail+=`
								<div class="am-u-sm-12 cl-p">
									<div class="order_key">包车类型：</div>
									<div class="order_value_black"></div>
								</div>
							`
						}else{
							
							detail+=`
								<div class="am-u-sm-12 cl-p">
									<div class="order_key">行程备注：</div>
									<div class="order_value_black"><input  type='text'/></div>
								</div>
							`
							 if(type==2){
								detail+=`
									<div class="am-u-sm-12 cl-p">
										<div class="order_key">返程时间：</div>
										<div class="order_value_black"></div>
									</div>
								`
							}
						}
						return detail
					}
					
					//订单状态，订单一口价内容
					function orderstate(obj){
						
						var state="";
						state+=`<div class="am-u-sm-12 cl-p">
								<div class="order_detail_state_time">${obj.order_time}</div>
								<div class="order_detail_state_value">已申请,等待报价</div>
							</div>`
						if(obj.order_state==21){
							
							$('.keep').css('display','block')
							$('.keepdis').css('display','none')
							$('input[name="car_num"]').val(obj.car_num)
							if(obj.car_data){
							var car_data1=JSON.parse(obj.car_data)
							
							for(var i=0;i<car_data1.length;i++){
								carnum=i+1
								addcar(i+1,car_data1[i])
							}
							}
						}
						if(obj.order_state==23){
							state+=`<div class="am-u-sm-12 cl-p">
								<div class="order_detail_state_time">${obj.price_time}</div>
								<div class="order_detail_state_value">已报价，待客户确认</div>
							</div>`
							$('.keep').css('display','none')
							$('.keepdis').css('display','block')
							$('input[name="price"]').val(obj.price)
							$('input[name="car_num"]').val(obj.car_num)
							$('input[name="total_amount"]').val(obj.total_amount)
							$('input[name="link_man"]').val(obj.link_man)
							$('input[name="link_phone"]').val(obj.link_phone)
							if(obj.car_data){
							var car_data1=JSON.parse(obj.car_data)
							
							for(var i=0;i<car_data1.length;i++){
								console.log(car_data1[i])
								carnum=i+1
								addcar(i+1,car_data1[i])
							}
							}
							
						}
						
							
						return state
					}
					//订单添加车辆
					function addcar(num, obj) {
						if(obj) {
							$('.allcar').append(`
								<div class="am-u-sm-12 cl-p quote_car_list" style='margin-bottom:5px'>
									<div class="am-u-sm-1" style="padding: 10px 0;">
										<div style="width:100%;text-align: right;">
											<div class="quote_detail_dashed"></div>
										</div>
									</div>
									<div class="am-u-sm-11">
										<div class="am-fl" style="margin-right: 10px;">
											<select name='type' style='height:37px'>
												<option value="">车型-不限</option>
												<option ${obj.type==2?"selected":" "} value="2">优选型</option>
												<option ${obj.type==3?"selected":" "} value="3">舒适型</option>
												<option ${obj.type==4?"selected":" "} value="4">豪华型</option>
												<option ${obj.type==9?"selected":" "} value="9">商务型</option>
												<option ${obj.type==5?"selected":" "} value="5">大巴</option>
												<option ${obj.type==6?"selected":" "} value="6">小巴</option>
												<option ${obj.type==7?"selected":" "} value="7">中巴</option>
												<option ${obj.type==8?"selected":" "} value="8">执法车</option>
											</select>
										</div>
										<div class="am-fl" style="margin-right: 10px;">
											<div style="text-align: center;width: 60px;line-height: 35px;display: inline-block;border: 1px solid #ccc;">第${num}辆</div>
										</div>
										
										<div class="am-fl" style="margin-right: 10px;">
											<select class='car_number' name='car_id'  data-am-selected="{maxHeight: 100,searchBox: 1}"  data-msg='请选择车辆'>
											
											</select>
										</div>
										<div class="am-fl" style="margin-right: 10px;">
											<select class='driver_id' name='driver_id' data-am-selected="{maxHeight: 100,searchBox: 1}"  data-msg='请选择司机'></select>
											
										</div>
									</div>
								</div>	
							`)
							

							caroption({page: 1,pagenum: 999999}, $('.car_number').eq(num-1), obj.car_id)
							driveroption({page: 1,pagenum: 999999}, $('.driver_id').eq(num-1), obj.driver_id)
						} else {
							var data2={page: 1,pagenum: 999999};
//							console.log(caroption({page: 1,pagenum: 999999}))
							$('.allcar').append(`
								<div class="am-u-sm-12 cl-p quote_car_list" style='margin-bottom:5px'>
									<div class="am-u-sm-1" style="padding: 10px 0;">
										<div style="width:100%;text-align: right;">
											<div class="quote_detail_dashed"></div>
										</div>
									</div>
									<div class="am-u-sm-11 ">
										<div class="am-fl" style="margin-right: 10px;">
											<select name='type' style='height:37px'>
												<option value="">车型-不限</option>
												<option value="2">优选型</option>
												<option value="3">舒适型</option>
												<option value="4">豪华型</option>
												<option value="9">商务型</option>
												<option value="5">大巴</option>
												<option value="6">小巴</option>
												<option value="7">中巴</option>
												<option value="8">执法车</option>
											</select>
										</div>
										<div class="am-fl" style="margin-right: 10px;">
											<div style="text-align: center;width: 60px;line-height:35px;display: inline-block;border: 1px solid #ccc;">第${num}辆</div>
										</div>
										
										<div class="am-fl" style="margin-right: 10px;">
											<select class='car_number' name='car_id' data-am-selected="{maxHeight: 100,searchBox: 1}" data-msg='请选择车辆'>
											${carlist}
											</select>
										</div>
										<div class="am-fl" style="margin-right: 10px;">
											<select class='driver_id' name='driver_id' data-am-selected="{maxHeight: 100,searchBox: 1}"  data-msg='请选择司机'>
											${driverlist}
											</select>
											
										</div>
									</div>
								</div>	
							`)
							if(obj) {
								if(obj.type) {
									$('select[name="type"]').val(obj.type)
								}
							}

//							caroption({page: 1,pagenum: 999999}, $('.car_number'))
//							driveroption({page: 1,pagenum: 999999}, $('.driver_id'))
						}

						$('.car_number').selected('enable')
						$('.driver_id').selected('enable')
						//		$('.driver').unbind()
						//		$('.driver').on('click',function(){
						//			choosecar("{:url('admin/driver/index',['op'=>'search'])}")
						//		});
						$('select[name="type"]').unbind()
						$('select[name="type"]').on('change', function() {
							if($(this).find("option:selected").val()){
								var data = {page: 1,pagenum: 999999,type: $(this).find("option:selected").text()}
							}else{
								var data = {page: 1,pagenum: 999999}
							}
							
							caroption(data, $(this).closest('.quote_car_list').find('.car_number'))
						})
						$('.car_type').unbind()
						$('.car_type').on('click', function() {
							choosecar("{:url('models/index',['op'=>'search'])}")
						});
						myScroll.refresh()
					}

					

				} else {
					Err(res.msg)
				}
				//设置页面高度

			},
			error: function() {
				Load(1)
				Err('请求失败')
			}
		});
	}
	$('.keep').on('click',function(){
		Load()
		var data={};
		car_data=[];
		var check=true
		$('#quote').find('input').each(function(){
			if($(this).attr('name')){
				if($(this).val()){
					data[$(this).attr('name')]=$(this).val()
				}else{
					Err($(this).data('msg'))
					check=false
				}
			}
		})
		$('.quote_car_list').each(function(){
			var car_data_list={}
			$(this).find('select').each(function(){
				if($(this).attr('name')){
					console.log($(this).attr('name'))
					if($(this).val()){
						console.log($(this).val())
						car_data_list[$(this).attr('name')]=$(this).val()
					}else{
						Err($(this).data('msg'))
						check=false
					}
				}
			})
			car_data.push(car_data_list)
			data.car_data=JSON.stringify(car_data)
		})
		if(check){
			console.log(data)
			data.uid="{$uid}"
			$.ajax({
				type:"post",
				url:"{:url('/adminapi/Bus/setBusPrice')}",
				data:data,
				success:function(res){
					Load(1)
					if(res.code==1){
						Msg('报价完成');
						window.history.go(0)
					}else{
						Err(res.msg)
					}
				},
				error:function(){
					Load(1)
					Err('请求失败')
				}
			});
		}else{
			Load(1)
		}
	})
	
	//车辆下拉选择
	function caroption(data,obj,id){
		var carload= layer.load(0, {
		  shade: [0.1,'#fff'] //0.1透明度的白色背景
		});
		$.ajax({
			type:'post',
			url:'{:url("adminapi/Driver/searchCar")}',
			data:data,
			success:function(res){
				layer.close(carload)
				var carlisto="";
				for(var opi=0;opi<res.data.list.length;opi++){
					if(id){
						if(res.data.list[opi].car_id==id){
							carlisto+=`<option selected value="${res.data.list[opi].car_id}">${res.data.list[opi].number}</option>`
						}else{
							carlisto+=`<option value="${res.data.list[opi].car_id}">${res.data.list[opi].number}</option>`
						}
					}else{
						carlisto+=`<option value="${res.data.list[opi].car_id}">${res.data.list[opi].number}</option>`
					}
					
				}
				if(obj){
					obj.html(carlisto)
				}else{
					carlist= carlisto
				}
			},
			error:function(){
				layer.close(carload)
				Err('请求失败')
				
			}
				
		})
	}
	//司机下拉
	function driveroption(data,obj,id){
		var driverload= layer.load(0, {
		  shade: [0.1,'#fff'] //0.1透明度的白色背景
		});
		$.ajax({
			type:'post',
			url:'{:url("adminapi/Driver/driverList")}',
			data:data,
			success:function(res){
				console.log(res)
				layer.close(driverload)
				var driverlisto="";
				for(var opi2=0;opi2<res.data.length;opi2++){
					if(res.data[opi2].driver_id==id){
						driverlisto+=`<option selected value="${res.data[opi2].driver_id}">${res.data[opi2].name}</option>`
					}else{
					driverlisto+=`<option value="${res.data[opi2].driver_id}">${res.data[opi2].name}</option>`
					}
				}
				if(obj){
					obj.html(driverlisto)
				}else{
					driverlist= driverlisto
				}
				
			},
			error:function(){
				layer.close(driverload)
				Err('请求失败')
				
			}
				
		})
	}
})